<template>
  <div id="app11">
    <header class="header">
    </header>

    <div class="flex-center">

      <main class="content">

        <!-- 当前时间 -->
        <section class="info-card card-time">
          <div class="card-content">
            <h2><i class="fas fa-clock card-icon"></i>当前时间</h2>
            <p>{{ currentTime }}</p>
          </div>
        </section>

        <!-- 天气信息 -->
        <!-- <section class="info-card card-weather">
          <div class="card-content">
            <h2><i class="fas fa-cloud-sun card-icon"></i>天气信息</h2>
            <p v-if="weather">{{ weather.main.temp }}°C, {{ weather.weather[0].description }}</p>
            <p v-else>加载中...</p>
          </div>
        </section> -->

        <!-- CPU温度 -->
        <section class="info-card card-cpu">
          <div class="card-content">
            <h2><i class="fas fa-microchip card-icon"></i>java内存使用大小</h2>
            <p>{{ cpuTemperature }}</p>
          </div>
        </section>

        <!-- 网速 -->
        <section class="info-card card-speed">
          <div class="card-content">
            <h2><i class="fas fa-wifi card-icon"></i>网速</h2>
            {{ measureBandwidth() }}
          </div>
        </section>

        <!-- IP定位 -->
        <section class="info-card card-location">
          <div class="card-content">
            <h2><i class="fas fa-map-marker-alt card-icon"></i>位置信息</h2>
            <p v-if="location">{{ location.province }}, {{ location.city }}</p>
            <p v-else>加载中...</p>
          </div>
        </section>

        <!-- 系统信息 -->
        <section class="info-card card-system" v-for="(value, key) in systemInfo" :key="key">
          <div class="card-content">
            <h2><i class="fas fa-desktop card-icon"></i>{{ key }}</h2>
            <p>{{ value }}</p>
          </div>
        </section>

      </main>

    </div>
  </div>
</template>
  
<script setup>
import { ref, reactive, onMounted } from 'vue';

// 初始化响应式变量
const currentTime = ref(new Date().toLocaleTimeString());
const weather = ref(null);
const cpuTemperature = ref('N/A'); // 模拟数据
const downloadSpeed = ref('...');
const uploadSpeed = ref('...');
const location = ref(null);
const batteryLevel = ref('...');
const systemInfo = reactive({
});

// 更新当前时间
setInterval(() => {
  currentTime.value = new Date().toLocaleTimeString();
}, 1000);

// 获取天气信息
onMounted(async () => {
  const apiKey = 'YOUR_OPENWEATHERMAP_API_KEY'; // 替换为你的API密钥
  const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=Beijing&units=metric&appid=${apiKey}`);
  weather.value = await response.json();
});
if (window.performance && performance.memory) {
  const memory = performance.memory;
  cpuTemperature.value = (memory.usedJSHeapSize / 1024 / 1024).toFixed(2) + 'MB'
} else {
  console.log('performance.memory API不可用');
}

function measureBandwidth() {
  // 获取所有资源加载时间记录
  const resources = performance.getEntriesByType("resource");
  let totalSize = 0;
  let totalTime = 0;

  resources.forEach(resource => {
    totalSize += resource.transferSize; // 可能需要使用transferSize，因为有些资源可能被压缩
    totalTime += resource.duration; // 资源加载时间
  });
  // 计算平均速度（以字节/秒为单位）
  const averageSpeed = (totalSize / 1024) / (totalTime / 1000); // KB/s
  return averageSpeed.toFixed(2) + "kB/s";
}

// 获取网速（此处使用简单的占位符，实际应用需要更复杂的实现）
downloadSpeed.value = '未知';
uploadSpeed.value = '未知';

// 获取IP定位
onMounted(async () => {
  const response = await fetch('https://restapi.amap.com/v3/ip?key=0113a13c88697dcea6a445584d535837');
  location.value = await response.json();
});

// 获取电池电量
navigator.getBattery().then(battery => {
  batteryLevel.value = Math.round(battery.level * 100);
  battery.addEventListener('levelchange', () => {
    batteryLevel.value = Math.round(battery.level * 100);
  });
});

// 获取系统信息
function getSystemInfo() {
  const userAgent = navigator.userAgent;
  systemInfo['操作系统'] = `${getOS(userAgent)}`;
  systemInfo['CPU核心数'] = `${navigator.hardwareConcurrency || '未知'}`;
  systemInfo['内存大小'] = `${navigator.deviceMemory ? navigator.deviceMemory + ' GB' : '未知'}`;
}

function getBrowserName(userAgent) {
  if (userAgent.includes('Firefox')) return 'Firefox';
  if (userAgent.includes('Edg')) return 'Edge';
  if (userAgent.includes('Chrome')) return 'Chrome';
  if (userAgent.includes('Safari')) return 'Safari';
  return 'Unknown Browser';
}

function getBrowserVersion(userAgent) {
  const match = userAgent.match(/(Chrome|Firefox|Edg|Safari)\/(\d+)/);
  return match ? match[2] : '未知';
}

function getOS(userAgent) {
  if (userAgent.includes('Win')) return 'Windows';
  if (userAgent.includes('Mac')) return 'macOS';
  if (userAgent.includes('Linux')) return 'Linux';
  if (userAgent.includes('Android')) return 'Android';
  if (userAgent.includes('iPhone')) return 'iOS';
  return 'Unknown OS';
}

onMounted(() => {
  getSystemInfo();
});
</script>
<style scoped>
@keyframes slideIn {
  0% {
    transform: translateX(-100%);
    /* 从屏幕左侧进入 */
  }

  100% {
    transform: translateX(0);
    /* 到达原始位置 */
  }
}

@keyframes bounce {
  0% {
    transform: translateY(0);
    /* 初始位置 */
  }

  50% {
    transform: translateY(0);
    /* 向上跳动 5px */
  }

  100% {
    transform: translateY(0);
    /* 回到原位 */
  }
}

.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  animation: slideIn 1s ease-out, bounce 0.5s ease-in-out 1s forwards;
  /* 组合动画 */
}

body {
  margin: 0;
  padding: 0;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: linear-gradient(135deg, #e0f7ff, #cce5ff);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: start;
  padding-top: 40px;
}

#app11 {
  position: absolute;
  background-image: url('@/assets/bg.png') !important;
  height: 100%;
  border-radius: 0px;
  width: 100%;
  background-size: 100%;
}

.header {
  text-align: center;
  width: 100%;
  margin-bottom: 40px;
}

.header h1 {
  font-size: 2.8em;
  color: #0057e7;
  letter-spacing: 1.2px;
  text-shadow: 0 2px 10px rgba(0, 87, 231, 0.2);
  margin-bottom: 10px;
}

.content {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 30px;
  width: 90%;
  max-width: 1400px;
  padding: 0 20px;
}

.info-card {
  background: rgba(255, 255, 255, 0.2);
  /* 更高透明度的白色背景 */
  backdrop-filter: blur(20px);
  /* 更强的模糊效果 */
  border-radius: 15px;
  /* 圆角 */
  border: 1px solid rgba(255, 255, 255, 0.8);
  /* 更明显的白色边框 */
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
  /* 更强的阴影效果，增加深度 */
  padding: 25px;
  transition: transform 0.3s ease-in-out, box-shadow 0.3s ease-in-out;
  position: relative;
  overflow: hidden;
  cursor: default;


}

.info-card:hover {
  transform: translateY(-10px);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
}

.card-icon {
  font-size: 1.5rem;
  margin-right: 10px;
  color: white;
  background-color: var(--card-bg-color);
  padding: 8px 10px;
  border-radius: 10px;
}

.card-content h2 {
  font-size: 1.5em;
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  color: #333;
}

.card-content p {
  font-size: 1.1em;
  color: #555;
  line-height: 1.6;
  margin: 0;
}

/* Loading 效果 */
.card-content p:empty::after,
.card-content p:only-child:empty::after {
  content: "加载中...";
  color: #888;
  animation: blink 1.5s infinite;
}

@keyframes blink {

  0%,
  100% {
    opacity: 1;
  }

  50% {
    opacity: 0.3;
  }
}

/* 不同卡片颜色定义 */
.card-time .card-icon {
  background-color: #4a90e2;
}

.card-weather .card-icon {
  background-color: #00c6ff;
}

.card-cpu .card-icon {
  background-color: #ff6b6b;
}

.card-speed .card-icon {
  background-color: #96c93d;
}

.card-location .card-icon {
  background-color: #fcb900;
}

.card-battery .card-icon {
  background-color: #7b4eff;
}

.card-system .card-icon {
  background-color: #00bcd4;
}

@media (max-width: 768px) {
  .content {
    grid-template-columns: 1fr;
  }

  .header h1 {
    font-size: 2em;
  }

  .info-card {
    padding: 20px;
  }

  .card-content h2 {
    font-size: 1.2em;
  }

  .card-content p {
    font-size: 1em;
  }
}</style>